﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace winformLesson1
{
    public partial class Register : Form
    {
        public Register()
        {
            InitializeComponent();
        }

        private void btn_Reg_Click(object sender, EventArgs e)
        {
            // 1、采集数据
            string username = tbx_UserName.Text.Trim();
            string userpwd = tbx_UserPwd.Text;
            string desc = tbx_Desc.Text.Trim();

            // 2、判断账号密码是否为空
            if (string.IsNullOrEmpty(username))
            {
                MessageBox.Show("账号不能为空！");
                return;
            }
            if (string.IsNullOrEmpty(userpwd))
            {
                MessageBox.Show("密码不能为空！");
                return;
            }

            // 3、判断账号是否存在
            if (UserNameIsExist(username))
            {
                MessageBox.Show("该用户已存在！");
                return;
            }

            // 4、开始注册
            if (RegisterUser(username, userpwd, desc))
            {
                this.Close();
            }
            else
            {
                MessageBox.Show("注册失败");
            }

        }


        public bool RegisterUser(string username, string userpwd, string desc)
        {
            string sql = string.Format("insert into [User] values(@UserName,@UserPwd,@Desc,@CreateTime)");
            SqlParameter[] paras = new SqlParameter[]
            {
                new SqlParameter("@UserName", username),
                new SqlParameter("@UserPwd", userpwd),
                new SqlParameter("@Desc", desc),
                new SqlParameter("@CreateTime", DateTime.Now.ToString("yyyy-MM-dd HH:ss:mm"))
            };
            return SqlHelper.ExecuteNonQuery(sql,paras) > 0 ? true : false;

            //// 1、准备连接字符串
            ////string connStr = "server=DESKTOP-111053T\\MSSQL; database=WebTest; Uid=sa; Pwd=598642004;";
            //string connStr = ConfigurationManager.AppSettings["connStr"];
            //// 2、准备sql 语句
            //string sql = string.Format("insert into [User] values(@UserName,@UserPwd,@Desc,@CreateTime)");
            //// 3、准备连接对象 SqlConnection
            //using (SqlConnection conn = new SqlConnection(connStr))
            //using (SqlCommand comm = new SqlCommand(sql, conn))
            //{
            //    comm.Parameters.AddRange(new SqlParameter[]
            //    {
            //        new SqlParameter("@UserName", username),
            //        new SqlParameter("@UserPwd", userpwd),
            //        new SqlParameter("@Desc", desc),
            //        new SqlParameter("@CreateTime", DateTime.Now.ToString("yyyy-MM-dd HH:ss:mm"))
            //    });

            //    conn.Open();
            //    return comm.ExecuteNonQuery() > 0 ? true : false;
            //}
        }


        public bool UserNameIsExist(string username)
        {
            string sql = string.Format("SELECT count(UserId) FROM [User] where UserName=@UserName");
            SqlParameter[] paras = new SqlParameter[] 
            {
                new SqlParameter("@UserName",username)
            };
            return SqlHelper.Exist(sql, paras);
            
            //// 1、准备连接字符串
            ////string connStr = "server=DESKTOP-111053T\\MSSQL; database=WebTest; Uid=sa; Pwd=598642004;";
            //string connStr = ConfigurationManager.AppSettings["connStr"];

            //// 2、准备连接对象 SqlConnection
            //int r = 0;

            //using (SqlConnection conn = new SqlConnection(connStr))
            //{
            //    // 3、准备sql 语句
            //    string sql = string.Format("SELECT count(UserId) FROM [User] where UserName=@UserName");
            //    // 4、 创建 SqlCommand 对象
            //    using (SqlCommand comm = new SqlCommand(sql, conn))
            //    {
            //        comm.Parameters.Add(new SqlParameter("@UserName", username));
            //        conn.Open();
            //        r = (int)comm.ExecuteScalar();
            //    }
            //}
            //return r == 0 ? false : true;
        }
    }
}
